<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <title>购物车</title>
    <link rel="stylesheet" href="__HOMECSS__/style.css">

    <script src="__HOMEJS__/jquery-1.7.2.min.js"></script>
</head>
<body>
<style>



</style>
{if condition="!empty($cartList['cartList'])"}
    <div class="shopping">
        <div class="shoppingList">
            {foreach name="cartList['cartList']" item="item"}
            <div class="shoppingBox">
                <div class="editbtn" name="edit">
                    <p>编辑</p>
                </div>
                <div class="ProductBox">
                    <div class="productImg">
                        <input type="checkbox" value="{$item.id}" class="selectPart" {if condition="$item.is_checked eq 1"}checked{/if} />
                        <div><img src="{$item.goods_image_small}" width="100%"/></div>

                    </div>
                    <a href="{:Url('Goods/detail',['id'=>$item['goods_id']])}" class="productTxt">
                        <p class="name">{$item.goods_name}</p>
                        <p class="attr">{$item.attr_key_name}</p>
                        <p class="price">¥ <span class="nuitPrice">{$item.price}</span> <span class="num">×<span class="numvalue">{$item.goods_num}</span> </span></p>
                    </a>

                    <div class="delectBtn" data-id="{$item.id}">
                        删除
                    </div>
                    <div class="chosenum">
                        <span class="cut">-</span>
                        <input type="text" data-id="{$item.id}" value="{$item.goods_num}"/>
                        <span class="add">+</span>
                        <div class="clear"></div>
                    </div>

                    <div class="clear"></div>
                </div>
            </div>
            {/foreach}
        </div>
    </div>
    <div class="bottomList">

        <div class="bottomBox inputBox">

            <input type="checkbox"  id="selectAll"/>
            <label>全选</label>

        </div>
        <!--<div class="bottomBox TotalBox">-->
        <!--<p>合计：<span>5.00</span></p>-->
        <!--</div>-->
        <div class="bottomBox">
            <div class="btn" id="confirm_cart">
                提交
            </div>
            <span>合计：<span style="color:red;font-weight: bold;" id="total_amount">￥ {$cartList.total_amount}</span></span>
        </div>
    </div>
    {else/}
    <div class="picture404">
        <img src="__HOMEIMG__/error.png" alt="">
        <p>购物车没有商品，快去<a href="{:Url('Index/index')}">挑选</a>吧</p>
    </div>
{/if}

{include file="Public:footer" /}

</body>
<script src="__HOMEJS__/jquery-1.7.2.min.js"></script>
<script src="__PUBLIC__/layer/layer.js"></script>
<script>
    window.onload = function(){
        shop_cart.init();
    }
    var shop_cart ={
        allPrice:0,
        init:function(){
            this.SelectAll();
            this.edit();
            this.choseNum();
            this.delect();
            this.confirm_cart();
        },
        SelectAll:function(){
            $("#selectAll").click(function(){
                var obj = this;
                var is_checked = 0;
                if(obj.checked){
                    is_checked = 1;
                }
                $.ajax({
                    url:"{:Url('Cart/changeAllChecked')}",
                    data:{is_checked:is_checked},
                    type:"post",
                    dataType:"json",
                    success:function(res){
                        if(res.code > 0){
                            if(obj.checked){
                                $("input[type='checkbox']").attr("checked","true");
                            }else{
                                $("input[type='checkbox']").removeAttr("checked");
                            }
                            $('#total_amount').text('￥ '+res.result.total_amount);
                        }else{
                            layer.msg(res.msg);
                        }
                    }
                });
            });
            var chknum = $(".selectPart").size();//选项总个数
            var chk =0;
            $(".selectPart").each(function(){
                if($(this).attr("checked")){
                    chk++;
                }
            });
            if(chknum==chk){
                //全选
                $("#selectAll").attr("checked",true);
            }else{
                //不全选
                $("#selectAll").attr("checked",false);
            }
            $(".selectPart").click(function(){
                var is_checked = 0;
                if(this.checked){
                    is_checked = 1;
                }
                $.ajax({
                    url:"{:Url('Cart/changeChecked')}",
                    data:{cart_id:$(this).val(),is_checked:is_checked},
                    type:"post",
                    dataType:"json",
                    success:function(res){
                        if(res.code > 0){
                            chk = 0;
                            $(".selectPart").each(function(){
                                if($(this).attr("checked")){
                                    chk++;
                                }
                            });
                            if(chknum==chk){
                                //全选
                                $("#selectAll").attr("checked",true);
                            }else{
                                //不全选
                                $("#selectAll").attr("checked",false);
                            }
                            $('#total_amount').text('￥ '+res.result.total_amount);
                        }else{
                            layer.msg(res.msg);
                        }
                    }
                });
            })
        },
        edit:function(){
            $(".editbtn").click(function(){
                var name = $(this).attr("name");
                var shoppingBox =  $(this).parents(".shoppingBox");

                if(name == "edit"){
                    $(this).attr("name","cancel");
                    $(this).find("p").text("保存");
                    shoppingBox.find(".delectBtn").css({
                        "right":"0px",
                    })
                    shoppingBox.find(".chosenum").show();
                    shoppingBox.find(".productTxt").hide();
                    shoppingBox.find(".num").hide();
                    shoppingBox.find(".chosenum input").val(shoppingBox.find(".numvalue").text());
                }else{
                    var obj = $(this);
                    $.ajax({
                        url:"{:Url('Cart/adjustCart')}",
                        data:{cart_id:shoppingBox.find(".chosenum input").data('id'),number:shoppingBox.find(".chosenum input").val()},
                        type:"post",
                        dataType:"json",
                        success:function(res){
                            if(res.code > 0){
                                obj.attr("name","edit");
                                obj.find("p").text("编辑");
                                shoppingBox.find(".delectBtn").css({
                                    "right":"-50px",
                                })
                                shoppingBox.find(".chosenum").hide();
                                shoppingBox.find(".productTxt").show();
                                shoppingBox.find(".num").show();
                                shoppingBox.find(".numvalue").text(shoppingBox.find(".chosenum input").val());
                                $('#total_amount').text('￥ '+res.result.total_amount);
                            }else{
                                layer.msg(res.msg);
                            }
                        }
                    })
                }
            })
        },
        choseNum:function () {
            $(".cut").click(function(){
                var num =parseInt($(this).parents(".chosenum").find("input").val());
                if(num != 0){
                    num --;
                }
                $(this).parents(".chosenum").find("input").val(num);
            })
            $(".add").click(function(){
                var num =parseInt($(this).parents(".chosenum").find("input").val());
                if(num != 999){
                    num ++;
                }
                $(this).parents(".chosenum").find("input").val(num);
            })
        },
        delect:function(){
            $(".delectBtn").click(function(){
                var obj = $(this);
                $.ajax({
                    url:"{:Url('Cart/delete')}",
                    data:{id:obj.data('id')},
                    type:"post",
                    dataType:"json",
                    success:function(res){
                        if(res.code > 0){
                            obj.parents(".shoppingBox").slideUp("slow");
                            $('#total_amount').text('￥ '+res.result.total_amount);
                        }else{
                            layer.msg(res.msg);
                        }
                    }
                });
            });
        },
        confirm_cart:function(){
            $('#confirm_cart').click(function(){
                var chk =0;
                $(".selectPart").each(function(){
                    if($(this).attr("checked")){
                        chk++;
                    }
                });
                if(chk == 0){
                    layer.msg('购物车里没有商品');
                    return false;
                }
                window.location.href="{:Url('Order/cart')}";
            });
        }

    }
</script>
</html>